package com.android.accessibility.fragment;

import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.android.accessibility.R;
import com.android.accessibility.activity.CourseActivity;
import com.android.accessibility.activity.HomeActivity;
import com.android.accessibility.ad.ForwardChecker;
import com.android.accessibility.base.BaseFragment;
import com.android.accessibility.service.WXObserverService;
import com.android.accessibility.utils.Constant;
import com.android.accessibility.utils.Log;
import com.android.accessibility.utils.Utils;
import com.android.accessibility.utils.VideoDirFinder;
import com.android.accessibility.utils.VideoDirFinder.OnFindListener;

public class OneKeyForwardFragment extends BaseFragment implements
android.view.View.OnClickListener, OnFindListener, OnDismissListener {

    private ToggleButton mWXForwardButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.onkeyforward_layout, null);
        initFunction(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        updateView();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        VideoDirFinder finder = new VideoDirFinder(getActivity());
        finder.setOnFindListener(this);
        finder.find();
    }

    private void updateView() {
        boolean isWork = Utils.isServiceWork(getActivity(),
                "com.android.accessibility.service.WXObserverService");
        mWXForwardButton.setChecked(isWork);
    }

    private void initFunction(View rootView) {
        View view = null;
        mWXForwardButton = (ToggleButton) rootView
                .findViewById(R.id.start_stop_forward);
        mWXForwardButton.setOnClickListener(this);

        view = rootView.findViewById(R.id.show_course);
        view.setOnClickListener(this);

        view = rootView.findViewById(R.id.start_wx);
        view.setOnClickListener(this);

        FrameLayout adLayout = (FrameLayout) rootView.findViewById(R.id.banner_adview);
        ForwardChecker.get(getActivity()).setBannerAdView(adLayout);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.start_stop_forward) {
            try {
                ToggleButton button = (ToggleButton) v;
                Intent intent = new Intent(getActivity(),
                        WXObserverService.class);
                if (button.isChecked()) {
                    getActivity().startService(intent);
                    ForwardChecker.get(getActivity()).showTipDialog();
                } else {
                    getActivity().stopService(intent);
                }
            } catch (Exception e) {
                Log.d(Log.TAG, "error : " + e);
            }
        } else if (v.getId() == R.id.start_wx) {
            Intent intent = getActivity().getPackageManager()
                    .getLaunchIntentForPackage(Constant.WX_PACKAGENAME);
            startActivitySafely(intent);
        } else if (v.getId() == R.id.show_course) {
            startActivity(new Intent(getActivity(), CourseActivity.class));
        }
    }

    private void startActivitySafely(Intent intent) {
        try {
            startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(getActivity(), "未找到相应的功能模块", Toast.LENGTH_SHORT).show();
        }
    }

    private void checkWxVideoDir() {
        List<String> list = Utils.readVideoDir(getActivity());
        if (list == null || list.size() <= 0) {
            return;
        }
        final String[] videoDir = new String[list.size()];
        list.toArray(videoDir);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setItems(videoDir, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d(Log.TAG, String.format("videoDir[%d] = %s", which,
                        videoDir[which]));
                Intent intent = new Intent(getActivity(),
                        HomeActivity.class);
                intent.putExtra("video_dir", videoDir[which]);
                startActivitySafely(intent);
            }
        });
        builder.setTitle("微信视频目录列表");
        builder.create().show();
    }

    @Override
    public void onFindComplete(List<String> videoDir) {
        Log.d(Log.TAG, "videoDir : " + videoDir);
        if (videoDir == null || videoDir.size() <= 0) {
            Toast.makeText(getActivity(), "无法获取微信的视频目录，您的手机暂时无法支持", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
    }
}
